﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Services;
using System.Globalization;
using Public;

namespace SalomonWebsite.Handler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetMenuColByParID : IHttpHandler
    {
        DataTable dtRtn = new DataTable();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int txbPKID = Convert.ToInt32(context.Request["PKID"].Trim(), CultureInfo.InvariantCulture);
            GetCatList(txbPKID);
            string str = "";
            if (dtRtn.Rows.Count > 0)
            {
                int n = 0;
                int tmp = 0;
                for (int i = 0; i < dtRtn.Rows.Count; i++)
                {

                    if (n == 2 && Convert.ToInt16(dtRtn.Rows[i]["CatRank"]) == 2)
                    {
                        str += "</div>";
                        n = 0;
                    }

                    if (Convert.ToInt16(dtRtn.Rows[i]["CatRank"]) == 2) // 2级分类
                    {
                        if (tmp == 3)
                        {
                            str += "</ul></div>";
                        }

                        if (n == 0)
                        {
                            str += "<div class=\"MenuCol\">";
                        }

                        if (Convert.ToInt32(dtRtn.Rows[i]["Num"]) != 0)
                        {
                            str += "<div><h3><a href=\"Cat-" + txbPKID + "-" + dtRtn.Rows[i]["PKID"].ToString() + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></h3>";
                            //str += "<div><h3><a href=\"ProCat.aspx?pkid=" + dtRtn.Rows[i]["PKID"].ToString() + "\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></h3>";
                        }
                        else
                        {
                            str += "<div><h3><a href=\"Ser-" + txbPKID + "-" + dtRtn.Rows[i]["PKID"].ToString() + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></h3>";
                            //str += "<div><h3><a href=\"ProSeries.aspx?pkid=" + dtRtn.Rows[i]["PKID"].ToString() + "\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></h3>";
                        }

                        n++;
                    }

                    if (Convert.ToInt16(dtRtn.Rows[i]["CatRank"]) == 3) // 3级分类
                    {
                        str += "<li><a href=\"Ser-" + txbPKID + "-" + dtRtn.Rows[i]["PKID"].ToString() + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></li>";
                        //str += "<li><a href=\"ProSeries.aspx?pkid=" + dtRtn.Rows[i]["PKID"].ToString() + "\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></li>";
                    }

                    if (Convert.ToInt32(dtRtn.Rows[i]["Num"]) != 0)
                    {
                        str += "<ul>";
                    }

                    tmp = Convert.ToInt16(dtRtn.Rows[i]["CatRank"]);

                }
            }
            else
            {
                str += "<div class=\"MenuCol\">&nbsp</div>";
            }

            context.Response.Write(str);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        protected void GetCatList(int ParID)
        {
            DataTable dt = Bll.Category.MULTIPLE(Global.Connection);
            if (dt.Rows.Count > 0)
            {

                // Add three column objects to the table.
                DataColumn dc1 = new DataColumn();
                dc1.DataType = System.Type.GetType("System.Int32");
                dc1.ColumnName = "PKID";
                //dc1.AutoIncrement = true;
                dtRtn.Columns.Add(dc1);

                DataColumn dc2 = new DataColumn();
                dc2.DataType = System.Type.GetType("System.String");
                dc2.ColumnName = "CatName";
                //dc2.AutoIncrement = true;
                dtRtn.Columns.Add(dc2);

               

                DataColumn dc3 = new DataColumn();
                dc3.DataType = System.Type.GetType("System.Int32");
                dc3.ColumnName = "ParID";
                //dc3.AutoIncrement = true;
                dtRtn.Columns.Add(dc3);

                DataColumn dc4 = new DataColumn();
                dc4.DataType = System.Type.GetType("System.Int32");
                dc4.ColumnName = "CatRank";
                //dc4.AutoIncrement = true;
                dtRtn.Columns.Add(dc4);

                DataColumn dc5 = new DataColumn();
                dc5.DataType = System.Type.GetType("System.Int32");
                dc5.ColumnName = "SortBy";
                //dc5.AutoIncrement = true;
                dtRtn.Columns.Add(dc5);

                DataColumn dc6 = new DataColumn();
                dc6.DataType = System.Type.GetType("System.Int32");
                dc6.ColumnName = "Num";
                //dc5.AutoIncrement = true;
                dtRtn.Columns.Add(dc6);


                DataColumn dc7 = new DataColumn();
                dc7.DataType = System.Type.GetType("System.String");
                dc7.ColumnName = "CatCode";
                //dc2.AutoIncrement = true;
                dtRtn.Columns.Add(dc7);

                GetCatData(ParID, dt, dtRtn);

                //if (dtRtn.Rows.Count > 0)
                //{
                    
                //}

            }
        }


        protected void GetCatData(int parID, DataTable dtCat, DataTable dtRtn)
        {
            DataRow[] drs = dtCat.Select(" [ParID] = " + parID);

            for (int i = 0; i < drs.Length; i++)
            {
                DataRow dr = dtRtn.NewRow();
                dr["PKID"] = Convert.ToInt32(drs[i]["PKID"]);
                dr["CatName"] = drs[i]["CatName"].ToString();
                dr["ParID"] = Convert.ToInt32(drs[i]["ParID"]);
                dr["CatRank"] = Convert.ToInt32(drs[i]["CatRank"]);
                dr["SortBy"] = Convert.ToInt32(drs[i]["SortBy"]);
                dr["Num"] = Convert.ToInt32(drs[i]["Num"]);
                dr["CatCode"] = drs[i]["CatCode"].ToString();
                dtRtn.Rows.Add(dr);
                GetCatData(Convert.ToInt32(drs[i]["PKID"]), dtCat, dtRtn);
            }
        }
    }
}
